package other.collection;

import java.util.PriorityQueue;

/**
 * @author chang.gui
 * @date 2022/3/28
 */
@SuppressWarnings("all")
public class 堆 {

    static class Node {
        int value;

        public Node(int _value) {
            value = _value;
        }
    }

    public static void main(String[] args) {
        // 常用方法
        // offer 增加元素，会自动调整
        // poll 弹出堆顶元素

        // 小根堆
        PriorityQueue<Node> smallHeap = new PriorityQueue<>((a, b) -> a.value - b.value);
        smallHeap.offer(new Node(6));
        smallHeap.offer(new Node(7));
        smallHeap.offer(new Node(3));
        smallHeap.offer(new Node(5));
        smallHeap.offer(new Node(0));
//        smallHeap.forEach(a -> System.out.print(a.value));
        while(!smallHeap.isEmpty()) {
            System.out.print(smallHeap.poll().value);
        }

        System.out.println();

        // 大根堆
        PriorityQueue<Node> bigHeap = new PriorityQueue<>((a, b) -> b.value - a.value);
        bigHeap.offer(new Node(6));
        bigHeap.offer(new Node(7));
        bigHeap.offer(new Node(3));
        bigHeap.offer(new Node(5));
        bigHeap.offer(new Node(0));
//        bigHeap.forEach(a -> System.out.print(a.value));
        while(!bigHeap.isEmpty()) {
            System.out.print(bigHeap.poll().value);
        }
    }

}
